#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

typedef struct ListNode {
    int val;
    struct ListNode* next;
}LN;

struct ListNode* removeElements(struct ListNode* head, int val) {
    if (head == NULL)
    {
        return head;
    }
    struct ListNode* n1 = head;
    while (n1->val == val)
    {
        head = n1->next;
        //free(n1);
        n1 = head;
        if (n1 == NULL)
            return head;
    }
    struct ListNode* n2 = head->next;
    while (n1 && n1->next)
    {
        while (n2->val == val)
        {
            n1->next = n2->next;
            //free(n2);
            n2 = n1->next;
            if (n2 == NULL)
                return head;
        }
        n1 = n2;
        n2 = n1->next;
    }
    return head;
}

int main()
{
    LN ln7 = { 6,NULL };
    LN ln6 = { 5,&ln7 };
    LN ln5 = { 4,&ln6 };
    LN ln4 = { 3,&ln5 };
    LN ln3 = { 6,&ln4 };
    LN ln2 = { 2,&ln3 };
    LN ln1 = { 1,&ln2 };
    LN* print = &ln1;
    while (print != NULL)
    {
        printf("%d ", print->val);
        print = print->next;
    }
    printf("\n");
    print = removeElements(&ln1, 6);
    while (print != NULL)
    {
        printf("%d ", print->val);
        print = print->next;
    }
    return 0;
}